
package com.starshineworks.entities;

import com.db4o.ObjectContainer;
import com.db4o.query.*;
import com.starshineworks.util.DbUtil;
import com.starshineworks.util.Util;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;

public class TeamE extends EntityE {

    public String    _class;
    public String    _age;
    public String    _gender;
    public Integer[] _manager;

    public TeamE() {
    }
    
    public TeamE(Integer id) {
        this._id = id;
    }
        
    public static List<Object> getTeamsByUserId(Integer uId) {
        ObjectContainer client = DbUtil.openClient();
        final Integer i = uId;        
        List<TeamE> result = client.query(new Predicate<TeamE>() {
            @Override
            public boolean match(TeamE t) {
                boolean x = false;
                for (int j = 0; j < 4; j++) {
                    x = x || t._manager[j].equals(i);
                }
                return x;
            }
        });        
        client.close();
        List tmp = result;
        List<Object> list  = (List<Object>)tmp;
        return list;        
    }
    
    private static boolean init_ = false;

     public static boolean init() {
        if (!init_) {
            
            UserE u = (UserE)EntityE.retrieve(new UserE()).get(0);
            
            Map<String,String[]> map = new HashMap<String,String[]>();
            String n1[] = {"Murmaids"};
            String c1[] = {"III"};
            String a1[] = {"U14"};
            String g1[] = {"Girls"};
            String m1[] = {u._id.toString(),null,null,null};
            map.put("_name", n1);
            map.put("_class", c1);
            map.put("_gender", g1);
            map.put("_age", a1);
            map.put("_manager", m1);
            create("TeamE", map);
            
            String n2[] = {"Elephants"};
            String c2[] = {"I"};
            String a2[] = {"U12"};
            String g2[] = {"Boys"};
            String m2[] = {u._id.toString(),null,null,null};
            map.put("_name", n2);
            map.put("_class", c2);
            map.put("_gender", g2);
            map.put("_age", a2);
            map.put("_manager", m2);
            create("TeamE", map);
          
            init_ = true;
            return true;
        }
        return false;
    }

     public String managerName() {
        return managerName(_manager[0]);
    }
     
    public String managerName(Integer id) {
        return UserE.fullName(id);
    }
             
    public static String teamName(Integer id)
    {
        String y = "(Not set)";
        try {
            TeamE u = new TeamE(id);
            TeamE x = (TeamE)EntityE.retrieve(u).get(0);
            return x._name;
        }
        catch (Exception e) {
            Util.handleException(e);
        } 
        return y;
    }
     
    @Override
    public String refreshDisplay(int view) {
        _display = "XXXX Name is " + _name;
        if (view != Util.HTMLViewNone) {
            ArrayList<String> keys = new ArrayList<String>();
            keys.add("Team name");
            keys.add("Team ID");
            keys.add("Manager");
            keys.add("Class");
            keys.add("Age");
            keys.add("Gender");
            ArrayList<String> vals = new ArrayList<String>();
            vals.add(_name);
            vals.add(_id.toString());
            vals.add(managerName() + " (User ID: "+_manager[0]+")");
            vals.add(_class);
            vals.add(_age);
            vals.add(_gender);
            
            if (view == Util.HTMLViewTable) {

                // Add button to request a field
                String bv = "Request field";
                String sc = "setSessionAttr(\"tId\","+_id+");window.location.href=\"/fm2/TeamPage\"";
                String br = "<input type=button value=\""+bv+"\" onclick='"+sc+";return false;' />";
                keys.add(br);
                vals.add("");

                List<List<String>> t = new ArrayList<List<String>>();
                t.add(vals);
                
                _display = Util.asHTMLTable(keys, t);
                return _display;
            }
            if (view == Util.HTMLViewForm) {

                ArrayList<String>   names = new ArrayList<String>();
                ArrayList<String>   types = new ArrayList<String>();
                ArrayList<String[]> opts = new ArrayList<String[]>();
                        
                names.add("_name");
                types.add("text");
                opts.add(null);

                names.add("_id");
                types.add("plain");
                opts.add(null);
                
                names.add("_manager");
                types.add("plain");
                opts.add(null);
               
                names.add("_class");
                types.add("select");
                String[] oClass = {"I","III","IV"};
                opts.add(oClass);
                
                names.add("_age");
                types.add("select");
                String[] oAge = {"U10","U12"};
                opts.add(oAge);
                
                names.add("_gender");
                types.add("select");
                String[] oGender = {"Boys","Girls"};
                opts.add(oGender);

                keys.add("");
                vals.add("Apply changes");
                //
                names.add("applychanges");
                types.add("submit");
                opts.add(null);

                /* XXXX
                keys.add("");
                vals.add("Request field");
                //
                String scr = "setSessionAttr(\"tId\","+_id+");window.location.href=\"/fm2/TeamPage\"";
                names.add(scr);
                types.add("button");
                opts.add(null);
                * */

                _display = Util.asHTMLForm("ManageTeam", 6, keys, names, vals, types, opts);
                return _display;
            }
        }
        return _display;
    }

}
